//
//  GeexRecorder.m
//  VideoRecordManager
//
//  Created by WangYonghe on 2019/12/31.
//  Copyright © 2019 WangYonghe. All rights reserved.
//

#import "GeexRecorder.h"
#import <MobileCoreServices/MobileCoreServices.h>
#import "GeexRecorderNetAccessor.h"

@interface GeexRecorder()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@end

@implementation GeexRecorder

+ (instancetype _Nonnull)shared {
    static dispatch_once_t onceToken;
    static GeexRecorder *shareInstance = nil;
    dispatch_once(&onceToken, ^{
        shareInstance = [[self alloc] init];
    });
    return shareInstance;
}

- (void)startWithWords:(NSString *)words controller:(UIViewController *)controller callBack:(VideoPathBlock)callBack{
    GeexVideoRecordVC *gc = [[GeexVideoRecordVC alloc]init];
    gc.words = words;
    if (self.videoTimes) {
        gc.customsTime = self.videoTimes;
    }
    [gc setFinishBlcok:^(id  _Nonnull object) {
        if (object) {
            callBack(object);
        }
    }];
//    [gc setFinishBlcok:^(NSString *path) {
//        if (path) {
//            callBack(path);
//        }
//    }];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:gc];
    nav.modalPresentationStyle = UIModalPresentationFullScreen;
    
    [controller presentViewController:nav animated:true completion:nil];
}

- (void)goSystemCameraController:(UIViewController *)controller{
    UIImagePickerController *pickerCon = [[UIImagePickerController alloc]init];
    pickerCon.sourceType = UIImagePickerControllerSourceTypeCamera;

    pickerCon.mediaTypes = @[(NSString *)kUTTypeMovie];//设定相机为视频
    pickerCon.cameraDevice = UIImagePickerControllerCameraDeviceFront;//设置相机后摄像头
    pickerCon.videoMaximumDuration = 10;//最长拍摄时间
    pickerCon.videoQuality = UIImagePickerControllerQualityTypeHigh;//拍摄质量

    pickerCon.allowsEditing = NO;//是否可编辑
    pickerCon.delegate = self;
    [controller presentViewController:pickerCon animated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    NSString *mediaType=[info objectForKey:UIImagePickerControllerMediaType];
    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {//如果是拍照
        UIImage *image;
        //如果允许编辑则获得编辑后的照片，否则获取原始照片
        if (picker.allowsEditing) {
            image=[info objectForKey:UIImagePickerControllerEditedImage];//获取编辑后的照片
        }else{
            image=[info objectForKey:UIImagePickerControllerOriginalImage];//获取原始照片
        }
        //self.resultImgView.image = image;
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);//保存到相簿
    }else if([mediaType isEqualToString:(NSString *)kUTTypeMovie]){//如果是录制视频
        NSLog(@"video...");
        NSURL *url = [info objectForKey:UIImagePickerControllerMediaURL];//视频路径
        NSString *urlStr = [url path];
        if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(urlStr)) {
            //保存视频到相簿，注意也可以使用ALAssetsLibrary来保存
            UISaveVideoAtPathToSavedPhotosAlbum(urlStr, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);//保存视频到相簿
        }
    }
    [picker dismissViewControllerAnimated:YES completion:nil];
}
//保存完成后调用的方法
- (void)video:(UIImage*)videoPath didFinishSavingWithError: (NSError *)error contextInfo: (void *)contextInfo {
    if (error) {
        NSLog(@"保存视频出错%@", error.localizedDescription);
    }
    else {
        NSLog(@"保存视频成功--%@",videoPath);
    }
}

@end
